<?php

require("../function/util.php");
// set timezone
$timezone = "Asia/Bangkok";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

$page = $_POST['page'];
$rp = $_POST['rp'];
$sortname = $_POST['sortname'];
$sortorder = $_POST['sortorder'];

if (!$sortname)
    $sortname = 'name';
if (!$sortorder)
    $sortorder = 'desc';
if (!$page)
    $page = 1;
if (!$rp)
    $rp = 10;

$start = (($page - 1) * $rp);
$limit = "LIMIT $start, $rp";
$sort = "ORDER BY $sortname $sortorder";

$where = "";

$query = addslashes($_POST['query']);
$qtype = $_POST['qtype'];

if ($query)
    $where = " WHERE $qtype LIKE '%$query%' ";
	
// fix query = 0 problem	
if ($query == "0")
    $where = " WHERE $qtype LIKE '%$query%' ";
	
$sql = "SELECT *
        FROM gallery
        $where $sort $limit";
$result = runSQL($sql);

$tableSql = "gallery";

$numrow = countRec('gallery_id', $tableSql, $where);

if ($numrow > 0) {
    $data['page'] = intval($page);
    $data['total'] = intval($numrow);
    while ($row = mysql_fetch_array($result)) {
        $rows[] = array(
            "id" => $row['gallery_id'],
            "cell" => array(
				$row['gallery_id'],
                $row['gallery_class'],
                $row['gallery_weight'],
                $row['gallery_image'],
				$row['gallery_description'],
				$row['gallery_status']	
            )
        );
    }
} else {
    $rows[] = array(
        "id" => 'null',
        "cell" => array(
            'null',
            'null',
            'null',
            'null',
            'null',		
			'null'
        )
    );
}

$data['rows'] = $rows;
echo json_encode($data);
exit;
?>